﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.ServiceBus;
using System.ServiceModel;
using OnPremiseProductService;

namespace RandomTester
{
    [TestClass]
    public class UnitTestServiceBus
    {
        [TestMethod]
        public void Host()
        {
            string[] args = new string[] { };
            // Determine the system connectivity mode based on the command line
            // arguments: -http, -tcp or -auto  (defaults to auto)
            ServiceBusEnvironment.SystemConnectivity.Mode = GetConnectivityMode(args);

            Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", "pijushbus", "HelliumService");

            // create the credentials object for the endpoint
            TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();
            sharedSecretServiceBusCredential.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider("owner", "SUu1kESNR3X/YlfgSfFMBWinVX+Y5IW5Jqpqb6hBJ4k=");

            // create the channel factory loading the configuration
            ChannelFactory<IProductsChannel> channelFactory = new BalancingChannelFactory<IProductsChannel>(new NetTcpRelayBinding(EndToEndSecurityMode.None, RelayClientAuthenticationType.RelayAccessToken), new EndpointAddress(serviceUri));

            // apply the Service Bus credentials
            channelFactory.Endpoint.Behaviors.Add(sharedSecretServiceBusCredential);



            HelliumRequest<string> cat = new HelliumRequest<string>() { Body = "food" };
            var channel = channelFactory.CreateChannel();

            channel.Open();
            var maol = channel.Ping("Cool");
            var products = channel.GetProducts("food");
          
            //var products = channel.GetProducts(cat);
            //var dc = products.Body;
            channel.Close();

            channelFactory.Close();
        }
        static ConnectivityMode GetConnectivityMode(string[] args)
        {
            foreach (string arg in args)
            {
                if (arg.Equals("/auto", StringComparison.InvariantCultureIgnoreCase) ||
                     arg.Equals("-auto", StringComparison.InvariantCultureIgnoreCase))
                {
                    return ConnectivityMode.AutoDetect;
                }
                else if (arg.Equals("/tcp", StringComparison.InvariantCultureIgnoreCase) ||
                     arg.Equals("-tcp", StringComparison.InvariantCultureIgnoreCase))
                {
                    return ConnectivityMode.Tcp;
                }
                else if (arg.Equals("/http", StringComparison.InvariantCultureIgnoreCase) ||
                     arg.Equals("-http", StringComparison.InvariantCultureIgnoreCase))
                {
                    return ConnectivityMode.Http;
                }
            }
            return ConnectivityMode.AutoDetect;
        }
    }
}
